package com.zw.hrm.service.impl;

import com.alibaba.fastjson.JSON;
import com.zw.hrm.dto.SMSSendDTO;
import com.zw.hrm.feignclients.RedisFeignClient;
import com.zw.hrm.service.IVerifyCodeService;
import com.zw.hrm.util.AjaxResult;
import com.zw.hrm.util.StrUtils;
import com.zw.hrm.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {
    @Autowired
    private RedisFeignClient redisFeignClient;
    @Override
    public String createImageCodeKey(String imageCodeKey) throws IOException {
        //验证参数
        if (!StringUtils.hasLength(imageCodeKey) || imageCodeKey == null){
            throw new RuntimeException("参数异常，生成验证图片失败");
        }
        //生成验证码值
        UUID uuid = UUID.randomUUID();
        String code = uuid.toString().substring(0, 4);
        //将验证码键值对放入redis
        redisFeignClient.setex(imageCodeKey, new Integer("300"), code);
        //调用工具生成验证码图片
        String imageCode = VerifyCodeUtils.verifyCode(135, 40, 4, code);
        return imageCode;
    }

    @Override
    public void sendSmsCode(Map<String,String> map) {
        //参数验证
        String imageCode = map.get("imageCode");
        String imageCodeKey = map.get("imageCodeKey");
        String phone = map.get("mobile");
        if(!StringUtils.hasLength(imageCode) || !StringUtils.hasLength(imageCodeKey) || !StringUtils.hasLength(phone)  ){
            throw new RuntimeException("无效的参数");
        }
        //从redis中查询验证码,查询结果验证
        AjaxResult ajaxResult = redisFeignClient.get(imageCodeKey);
        if (!ajaxResult.isSuccess() || ajaxResult.getResultObj() == null){
            throw new RuntimeException("无效的图片验证码，请重新发送");
        }
        //对比验证码
        String val = ajaxResult.getResultObj().toString();
        if (!val.equals(imageCode)){
            throw new RuntimeException("错误的图片验证码，请重新发送");
        }
        //短信验证码的KEY
        String phoneKey = "sms:" + phone;
        Date nowDate = new Date();
        //验证码
        String smsCode = null;
        //从redis中查看，是否已经存在
        AjaxResult smsAjaxResult = redisFeignClient.get(phoneKey);
        if (smsAjaxResult.isSuccess() && smsAjaxResult.getResultObj() != null){
            SMSSendDTO lastSmsSendDTO = JSON.parseObject(smsAjaxResult.getResultObj().toString(), SMSSendDTO.class);
            if((nowDate.getTime() - lastSmsSendDTO.getSendTime()) < 60000){
                throw new RuntimeException("发送频繁，请稍后重新发送");
            }
            // -> 使用上一次发送验证码继续发
            smsCode = lastSmsSendDTO.getCode();
        } else {
            //生成手机验证码
            smsCode = StrUtils.getRandomString(6);
        }

        //存储验证码发送记录到Redis(或者覆盖：改时间) ：时间，验证码，手机
        String senDTOJson = JSON.toJSONString(new SMSSendDTO(smsCode, phone, nowDate.getTime()));
        AjaxResult result = redisFeignClient.setex(phoneKey, 300, senDTOJson);

        //调用第三方接口发送短信，并将短信验证码存到redis中
        System.out.println("您的注册验证码为：["+smsCode+"]，请在五分钟内使用[BaiyT]");

    }
}
